package cn.biplam.front.component.view.systemManage.modelset.manage;

import cn.biplam.common.bean.md.MdTheme;
import cn.biplam.common.bean.md.ModelThemeRootObj;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.mdManage.TypeCode.OptModelIndexCode;
import cn.biplam.common.paramters.ModelType.Model_Object_Type;
import cn.biplam.common.paramters.ModelType.Model_Type;
import cn.biplam.common.struc.BaseStruc;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.view.common.DbSourceSelect;
import cn.biplam.front.component.view.mdManage.ThemeSelect;
import cn.biplam.front.component.view.systemManage.modelset.ModelSetManage;
import cn.biplam.front.html.HtmlText;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Length_Unit;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.ScriptCustomer;
import cn.biplam.front.htmlscript.script.ScriptPrompt.Close_Prompt;
import cn.biplam.front.template.base.SimpleInput;
import cn.biplam.front.template.base.SimpleTextArea;
import cn.biplam.front.template.control.FlatTable;

public class ModelThemeModify extends AjxResponse{
	
	/** 组件ID */
	public static String cId="8207";
	/** 获取修改模型主题界面动作ID */
	public static String Modify_Theme="820701";
	/** 修改模型主题操作动作ID */
	public static String Modify_Theme_Opt="820702";
	private String width = "420";
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(actionId.equals(Modify_Theme)){
			return _getModifyTheme();
		}else if(actionId.equals(Modify_Theme_Opt)){
			return _modifyOpt();
		}else{
			throw new ErrorMessage("Ajx Exchange outport not found:"+ actionId+" !");
		}
	}
	
	/**
	 * 获取添加数据源界面
	 * @return
	 * @throws ErrorMessage
	 */
	private HtmlWindow _getModifyTheme() throws ErrorMessage{
		String themeId = recordId;
		String modelType = params.getByKey("modelType");
		ModelThemeRootObj mtroi = ModelThemeRootObj.getNewInstance(mUser,modelType);
		HtmlWindow panel=_drawPanel(mUser,getThemeByID(themeId, modelType),sHomeUrl,modelType,themeId);
//		ScriptCustomer.getSpSubmitCheckInput(getFieldCheck(mtroi),panel,cId, Modify_Theme_Opt,containerId,Is_RefreshParent.NoRefresh,
//				Is_AddParam.NoAdd,Close_Prompt.Close);
		ScriptCustomer.getSpSubmitCheckInput(getFieldCheck(mtroi),panel,cId, Modify_Theme_Opt,Close_Prompt.Close);
		panel.setStyleWidth(width);
		panel.put(new HtmlValue("h,modelType","h,modelType",modelType));
		panel.put(new HtmlValue("h,"+mtroi.getTHEME_TYPE(),
				                "h,"+mtroi.getTHEME_TYPE(),
				                params.getByKey(mtroi.getTHEME_TYPE())));
		return panel;
	}
	
	/**
	 * 数据源信息添加或编辑界面 
	 * @throws ErrorMessage
	 */
	private HtmlWindow _drawPanel(UserInfo mUser,OperatorResult sr,String sHomeUrl,String modelType,String themeId) throws ErrorMessage{
		String themeName=null;
		String themeDesc = null;
		String dsid=null;
		ModelThemeRootObj mtroi = ModelThemeRootObj.getNewInstance(mUser,modelType);
		if(TOOLS.exist(sr)){
			themeName = sr.getValue(mtroi.getTHEME_NAME());
			themeDesc = sr.getValue(mtroi.getTHEME_DESC());
			dsid = sr.getValue(mtroi.getDSID());			
		}
		
		FlatTable table = new FlatTable(cId,recordId);
		
		table.putTh("主题名称",0,0);
		table.putTd(new SimpleInput(mtroi.getTHEME_NAME(),"52",themeName).get(), 0, 1);
		table.putTh("主题说明",1,0);
		table.putTd(new SimpleTextArea(mtroi.getTHEME_DESC(),"50",themeDesc).get(),1,1);
		table.putTh("数据源",2,0);
		table.putTd(DbSourceSelect.get(mtroi.getDSID(), dsid, mUser, sHomeUrl), 2, 1);
		table.putTh("替换序号为",3,0);
		table.putTd(ThemeSelect.getRootExc(mtroi.getTHEME_ID()+"c", "", mUser, sHomeUrl, modelType, themeId), 3, 1);
		
		table.setColWidth(0,"20",Length_Unit.percent);
		table.setWidth(width);
		return table.get();
	}
	
	private HtmlWindow _modifyOpt() throws ErrorMessage{
		HtmlWindow win=new HtmlText();
		/** 模型类型*/
		String modelType = params.getByKey("modelType");
		ModelThemeRootObj mtro = ModelThemeRootObj.getNewInstance(mUser,modelType);		
		DataFilter filter=new DataFilter();
		filter.put(mtro.getTHEME_ID(),recordId);
		filter.put(mtro.getDSID(), params.getByKey(mtro.getDSID()+"value"));
		filter.putAll(this.params);
		/** 更新主题信息*/
		mtro.update(filter);
		win.bind(AjxScript.getA(ModelSetManage.cId,ModelSetManage.aId_main,modelType));
		/** 更新主题序号*/
		String themeIdc = params.getByKey(mtro.getTHEME_ID()+"c"+"value");
		if(themeIdc!=null && !"".equals(themeIdc.trim())){
			this.changeThemeOrid(recordId,themeIdc, modelType);
		}
		return win;
	}
	
	/**
	 * 根据主题id取主题的信息
	 * @param themeId
	 * @return
	 * @throws ErrorMessage 
	 */
	private OperatorResult getThemeByID(String themeId,String modelType) throws ErrorMessage{
		MdTheme mto = MdTheme.getNewInstance(mUser,themeId,modelType);
		return mto.getSelectResult();
	}
	
	/**
	 * 序号替换 
	 * @param themeId  主题id
	 * @param themeIdc 替换序号为主题id
	 * @param modelType 模型类型
	 * @throws ErrorMessage
	 */
	private void changeThemeOrid(String themeId,String themeIdc,String modelType) throws ErrorMessage{
		/** 当前主题序号*/
		String orid = null;
		/** 需要替换的主题序号*/
		String oridc = null;
		/** 当前主题是否有哥哥弟弟*/
		String [] have = this.getHaveLBrother(themeId, modelType);
		String h_Brother = have[0];
		String h_LBrother = have[1];
		/** 需要替换的主题是否有哥哥弟弟*/
		have = this.getHaveLBrother(themeIdc, modelType);
		String h_Brotherc = have[0];
		String h_LBrotherc = have[1];
		/** 取主题的序号*/
		MdTheme mto =MdTheme.getNewInstance(mUser,themeId,Model_Type.Src.toString());
		orid = mto.getOrId();
		mto = MdTheme.getNewInstance(mUser,themeIdc,Model_Type.Src.toString());
		oridc = mto.getOrId();
		/** 更新主题序号*/
		/**当前主题表*/
		ModelThemeRootObj mtro = ModelThemeRootObj.getNewInstance(mUser,modelType);
		DataFilter filter=new DataFilter();
		filter.put(mtro.getTHEME_ID(),themeId);
		filter.put(mtro.getORID(),oridc );
		filter.put(BaseStruc.IndexTableStruc.HAVE_BROTHER, h_Brotherc);
		filter.put(BaseStruc.IndexTableStruc.HAVE_L_BROTHER, h_LBrotherc);

		mtro.update(filter);
		/**需要替换序号的主题*/
		filter=new DataFilter();
		filter.put(mtro.getTHEME_ID(),themeIdc);
		filter.put(mtro.getORID(),orid );
		filter.put(BaseStruc.IndexTableStruc.HAVE_BROTHER, h_Brother);
		filter.put(BaseStruc.IndexTableStruc.HAVE_L_BROTHER, h_LBrother);

		mtro.update(filter);
		
	}
	
	/**
	 * 取需要检查的表单
	 * @return
	 * @throws ErrorMessage
	 */
	private KvMap<Integer,String[]> getFieldCheck(ModelThemeRootObj mtroi) throws ErrorMessage{
		KvMap<Integer,String[]> fields = new KvMap<Integer,String[]>();
		fields.put(0,new String[]{mtroi.getTHEME_NAME(), "请输入主题名称"});
		fields.put(1,new String[]{mtroi.getTHEME_DESC(), "请输入主题描述"});
		fields.put(2,new String[]{"h,"+mtroi.getDSID()+"value", "请选择数据源"});
		return fields;
	}
	
	/**
	 * 取节点是否有弟弟信息
	 * @param themeId   主题ID
	 * @param modelType 模型类型
	 * @return String
	 * @throws ErrorMessage
	 */
	private String[] getHaveLBrother(String themeId,String modelType) throws ErrorMessage{
		String[] haveb = new String[2];
		DataFilter filter = new DataFilter();
		filter.put(BaseStruc.IndexTableStruc.NODE_TYPE,modelType);
		filter.put(BaseStruc.IndexTableStruc.NODE_CODE,themeId);
		filter.put(BaseStruc.IndexTableStruc.OPERATOR_TYPE,Model_Object_Type.ROOT.toString());
		
		OperatorResult sr = Operator.get(OptModelIndexCode.GetBy_RealId_And_ModelType_And_OptType, 
				                    mUser.getUserID(),filter);
		if(TOOLS.exist(sr)){
			haveb[0] = sr.getValue(BaseStruc.IndexTableStruc.HAVE_BROTHER);
			haveb[1] = sr.getValue(BaseStruc.IndexTableStruc.HAVE_L_BROTHER);
		}
		return haveb;
	}
}
